// pages/personal/aftersale/aftersale.js
var utils = require('../../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: ['多拍不想要', '缺货', '坏果', '少发', '其他'],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            id: options.id
        })
        that.onOrderList(options.id);
    },
    //商品列表
    onOrderList: function(id) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/order/my_order_detail", {
            order_id: id
        }, function(res) {
            utils.hideLoading();
            that.setData({
                info: res.datas.snap_items,
            })
        })

    },
    //退款原因
    bindPickerChange(e) {
        var that = this;
        this.setData({
            index: e.detail.value,
            reason: that.data.array[e.detail.value]
        })
    },
    //退款说明
    onTextareaTap: function(e) {
        var that = this
        that.setData({
            content: e.detail.value
        })
    },
    //图片上传
    uploadImg: function(e) {
        var that = this;
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
                    header: {
                        'Content-Type': 'multipart/form-data'
                    },
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: app.globalData.key
                    },
                    success: function(result) {
                        utils.hideLoading();
                        var image_all = that.data.image_all || [];
                        //上传图片后返回的是字符串，需json.prise一下
                        var res = JSON.parse(result.data);
                        image_all.push(res);
                        that.setData({
                            image_all: image_all,
                        });
                    },
                    fail: function(res) {
                        utils.hideLoading();
                        utils.message('上传服务器上传失败,请稍后再试2');
                        console.log(res);
                    }
                })
            }
        })
    },
    //删除评价图片
    onCloseImgTap: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var image_all = that.data.image_all;
        for (var i in image_all) {
            if (image_all[i].id == image_all[index].id) {
                image_all.splice(i, 1)
            }
        }
        that.setData({
            image_all: image_all,
        });
    },

    //申请售后提交数据
    formSubmit: function(e) {
        var that = this;
        console.log(that.data.reason)
        console.log(that.data.content)
        console.log(that.data.image_all)
        console.log()
        var ppp = [];
        for (var i in that.data.image_all) {
            ppp.push(that.data.image_all[i].id);
        }
        if (that.data.reason != undefined && e.detail.value.nameNum != "") {
            utils.showLoading();
            utils.http("/api/v1/order/order_back", {
                order_id: that.data.id,
                images: ppp.join(','),
                content: that.data.content,
                goods_num: e.detail.value.nameNum,
                reason: that.data.reason
            }, function(res) {
                utils.hideLoading();
                if (res.state == 1) {
                    wx.showToast({
                        title: '申请成功',
                        duration: 2000
                    })
                    wx.navigateBack({})
                }else{
                    utils.message(res.datas)
                }
            })
        } else {
            wx.showToast({
                title: '退款原因和退款数量是必填的哦~',
                icon:"none",
                duration: 2000
            })
        }
    }
})